package com.market.cash.ui.activity

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.graphics.Color
import android.graphics.Typeface
import android.text.SpannableString
import android.text.Spanned
import android.text.style.ForegroundColorSpan
import android.text.style.StyleSpan
import android.view.View
import com.market.cash.R
import com.market.cash.databinding.CashApplySmsDataBinding
import com.market.cash.pro.ProActivity
import com.market.cash.utils.event.CashRootEventType
import com.market.cash.utils.helper.CashRootCustomConfigsHelper
import com.market.cash.utils.helper.CashRootProcessHelper
import com.market.cash.utils.thread.TimingThread

/**
 * CashRoot
 * @Author wwdeng
 * @CreateTime: 2024/10/24 18:45
 * @Annotation：申请贷款等待
 */
class ApplySmsDataActivity: ProActivity(), TimingThread.ITimingThreadListener {
    lateinit var binding: CashApplySmsDataBinding
    private var timeThread: TimingThread? = null
    //等待时长
    private var countdown = 9
    var eventRemark = ""
    companion object {
        private const val INTENT_KEY_PRODUCT = "product"
        private const val INTENT_KEY_TOKEN = "token"
        fun start(context: Context, pName: String, pToken: String) {
            val intent = Intent(context, ApplySmsDataActivity::class.java)
            intent.putExtra(INTENT_KEY_PRODUCT, pName)
            intent.putExtra(INTENT_KEY_TOKEN, pToken)
            context.startActivity(intent)
        }
    }

    override fun getCashRootRootView(): View {
        binding = CashApplySmsDataBinding.inflate(layoutInflater)
        return binding.root
    }

    override fun initCashRootView() {
        rootViewBinding.rootLayout.setBackgroundColor(Color.parseColor("#FFFFFF"))
        setCashRootTitleBarVisibility(View.VISIBLE)
        setCashRootTextTitle("")
        setCashRootBackBtnVisibility(View.INVISIBLE)
        setCashRootRightBtnRes(R.mipmap.cash_kefu_black)

        val textSMS = getString(R.string.loan_apply_wait_sms)
        val showText = String.format(getString(R.string.loan_apply_wait_tips),textSMS)

        val smsIndex = showText.indexOf(textSMS)
        val spString = SpannableString(showText)
        spString.setSpan(ForegroundColorSpan(Color.parseColor("#FF4D3E")), smsIndex, smsIndex + textSMS.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
        spString.setSpan(StyleSpan(Typeface.BOLD), smsIndex, smsIndex + textSMS.length, Spanned.SPAN_INCLUSIVE_INCLUSIVE)
        binding.textWaitTips.text = spString
    }

    override fun initCashRootListener() {
        //联系客服
        rootViewBinding.imgKefu.setOnClickListener {
            KefuActivity.start(this,eventRemark,loadProductName(),loadProductToken())
            addCashRootEventPoint(CashRootEventType.CLICK.CASHROOT_CLICK_CUSTOMER,eventRemark)
        }
    }

    override fun initCashRootData() {
        startTimeThread()
        //牌照信息
        if (!CashRootCustomConfigsHelper.isCashRootShowLicense()){
            binding.licenseLayout.removeAllViews()
            binding.licenseLayout.visibility = View.GONE
        } else {
            binding.licenseLayout.removeAllViews()
            binding.licenseLayout.addView(initCashRootLicenseView())
            binding.licenseLayout.visibility = View.VISIBLE
        }
    }

    /**
     * 启动倒计时
     */
    private fun startTimeThread() {
        if (timeThread != null) {
            timeThread!!.stopThread()
        }
        //随机 10秒 等待时长
        countdown = 9
        showCashRootTipsText()
        if (timeThread == null) {
            timeThread = TimingThread()
            timeThread!!.setThreadDelayMillis(1000)
            timeThread!!.setLoadListener(this)
        }
        timeThread!!.startThread()
    }

    override fun onLoading() {
        if (countdown <= 0){
            countdown = 9
            timeThread!!.stopThread()
            showCashRootLoanApplySuccess()
            return
        }
        countdown--
        showCashRootTipsText()
    }

    /**
     * 设置文本
     */
    @SuppressLint("SetTextI18n")
    private fun showCashRootTipsText(){
        binding.textCountdown.text = "0${countdown}"
        showCashRootStepStatusView()
    }

    /**
     * 贷款订单创建成功
     */
    private fun showCashRootLoanApplySuccess(){
        //需要校验 WhatsApp 或 SMS
        if (CashRootProcessHelper.isCheckWhatsApp() || CashRootProcessHelper.isCheckSMS()){
            VerificationActivity.start(this,loadProductName(),loadProductToken())
        }
        finish()
    }

    /**
     * 显示状态
     */
    private fun showCashRootStepStatusView(){
        if (countdown in 7..9){
            binding.imgStep1.setImageResource(R.mipmap.cash_sms_data_progress_icon)
            binding.textStep1.setTextColor(Color.parseColor("#000000"))
            binding.vStep1Bottom.setBackgroundColor(Color.parseColor("#8C8EAF"))

            binding.imgStep2.setImageResource(R.mipmap.cash_sms_data_wait_icon)
            binding.textStep2.setTextColor(Color.parseColor("#8C8EAF"))
            binding.vStep2Top.setBackgroundColor(Color.parseColor("#8C8EAF"))
            binding.vStep2Bottom.setBackgroundColor(Color.parseColor("#8C8EAF"))

            binding.imgStep3.setImageResource(R.mipmap.cash_sms_data_wait_icon)
            binding.textStep3.setTextColor(Color.parseColor("#8C8EAF"))
            binding.vStep3Top.setBackgroundColor(Color.parseColor("#8C8EAF"))
            return
        }

        if (countdown in 4..6){
            binding.imgStep1.setImageResource(R.mipmap.cash_sms_data_complete_icon)
            binding.textStep1.setTextColor(Color.parseColor("#8C8EAF"))
            binding.vStep1Bottom.setBackgroundColor(Color.parseColor("#B2F0CA"))

            binding.imgStep2.setImageResource(R.mipmap.cash_sms_data_progress_icon)
            binding.textStep2.setTextColor(Color.parseColor("#000000"))
            binding.vStep2Top.setBackgroundColor(Color.parseColor("#B2F0CA"))
            binding.vStep2Bottom.setBackgroundColor(Color.parseColor("#8C8EAF"))

            binding.imgStep3.setImageResource(R.mipmap.cash_sms_data_wait_icon)
            binding.textStep3.setTextColor(Color.parseColor("#8C8EAF"))
            binding.vStep3Top.setBackgroundColor(Color.parseColor("#8C8EAF"))
            return
        }
        binding.imgStep1.setImageResource(R.mipmap.cash_sms_data_complete_icon)
        binding.textStep1.setTextColor(Color.parseColor("#8C8EAF"))
        binding.vStep1Bottom.setBackgroundColor(Color.parseColor("#B2F0CA"))

        binding.imgStep2.setImageResource(R.mipmap.cash_sms_data_complete_icon)
        binding.textStep2.setTextColor(Color.parseColor("#8C8EAF"))
        binding.vStep2Top.setBackgroundColor(Color.parseColor("#B2F0CA"))
        binding.vStep2Bottom.setBackgroundColor(Color.parseColor("#B2F0CA"))

        if (countdown <= 2){
            binding.imgStep3.setImageResource(R.mipmap.cash_sms_data_complete_icon)
            binding.textStep3.setTextColor(Color.parseColor("#B2F0CA"))
            binding.vStep3Top.setBackgroundColor(Color.parseColor("#B2F0CA"))
            return
        }
        binding.imgStep3.setImageResource(R.mipmap.cash_sms_data_progress_icon)
        binding.textStep3.setTextColor(Color.parseColor("#000000"))
        binding.vStep3Top.setBackgroundColor(Color.parseColor("#B2F0CA"))
    }

    /**
     * 获取查看的产品名称
     */
    private fun loadProductName(): String {
        return intent.extras!!.getString(INTENT_KEY_PRODUCT).toString()
    }

    /**
     * 获取查看的产品Token
     */
    private fun loadProductToken(): String {
        return intent.extras!!.getString(INTENT_KEY_TOKEN).toString()
    }

    override fun onStart() {
        super.onStart()
        eventRemark = "${loadProductName()} Loan SMS Preview Page"
        addCashRootEventPoint(CashRootEventType.PAGE.CASHROOT_PAGE_LOAN_SMS_PREVIEW_START, eventRemark)
    }

    override fun onDestroy() {
        super.onDestroy()
        addCashRootEventPoint(CashRootEventType.PAGE.CASHROOT_PAGE_LOAN_SMS_PREVIEW_END, eventRemark)
        if (timeThread != null) {
            timeThread!!.stopThread()
            timeThread = null
        }
    }
}